<template>
  <div class="login-container">
    <h2>这是登录页</h2>
    <form @submit.prevent="handleSubmit" class="login-form">
      <div class="form-group">
        <label for="username">用户名:</label>
        <input type="text" id="username" v-model="username" required />
      </div>
      <div class="form-group">
        <label for="password">密码:</label>
        <input type="password" id="password" v-model="password" required />
      </div>
      <div class="form-group">
        <label for="agree">
          <input type="checkbox" id="agree" v-model="agree" />
          我同意相关协议
        </label>
      </div>
      <button type="submit" :disabled="!agree" class="submit-button">登录</button>
    </form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

// 定义状态变量
const username = ref('');
const password = ref('');
const agree = ref(false);

// 处理表单提交
const handleSubmit = async () => {
  if (!agree.value) return;

  try {
    const response = await fetch('https://apifoxmock.com/m1/4840030-4495188-default/login/password', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        username: username.value,
        password: password.value
      })
    });

    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error('请求失败:', error);
  }
};
</script>

<style scoped>
.login-container {
  width: 300px;
  margin: 50px auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

.form-group label {
  margin-bottom: 5px;
}

.form-group input[type="text"],
.form-group input[type="password"] {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 3px;
}

.submit-button {
  padding: 10px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

.submit-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
</style>